package com.ego.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ego.mapper.CategoryMapper;
import com.ego.pojo.Category;
import com.ego.service.CategoryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Ares
 * @date 2020-11-20
 */
@Service
public class CategoryServiceImpl implements CategoryService {

    @Resource
    private CategoryMapper categoryMapper;

    /**
     * 根据parentId查询子类目
     * @param pid 父ID
     * @return 种类集合
     */
    @Override
    @Transactional(readOnly = true,rollbackFor = Exception.class,propagation = Propagation.SUPPORTS)
    public List<Category> queryCategoryListByParentId(Long pid) {
        QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", pid);
        return this.categoryMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer save(Category category) {
        return categoryMapper.insert(category);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer delete(Long id) {
        return categoryMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class,propagation = Propagation.REQUIRED)
    public Integer update(Category category) {
        return categoryMapper.updateById(category);
    }

    @Override
    public List<Long> selectById(Long bid) {
        return categoryMapper.selectCategoryBrandById(bid);
    }

    @Override
    public Category selectCategoryById(Long aLong) {
        return categoryMapper.selectById(aLong);
    }

    @Override
    @Transactional(readOnly = true)
    public List<Category> selectByIds(List<Long> idList) {
        return categoryMapper.selectBatchIds(idList);
    }
}
